#!/bin/bash

echo "Content-Type:text/html;charset=utf-8"
echo
#加载功能模块
source func/env.sh

source func/db.sh
source func/loginCheck.sh
source func/tools.sh

#写日志
log INFO "请求信息"
checkLogin token

#传入动作
action=$(echo $QUERY_STRING | awk -F '=' '{print $2}')

case $action in
login)
    View=$(cat template/login.html)
    eval "cat <<EOF
$(<template/base-login.html)
EOF"

    ;;

add_template)

    View=$(eval "cat <<EOF
$(<form/add_template.html)
EOF
")

    eval "cat <<EOF
    $(<template/base.html)
EOF"

    ;;

add_host)
    getGroup

    View=$(eval "cat <<EOF
$(<form/add_host.html)
EOF
")
    eval "cat <<EOF
    $(<template/base.html)
EOF"
    ;;
show_task)
    Callback() {
        eval "cat <<EOF
    <tr>
        <td>$1</td>
        <td>$2</td>
        <td>$3</td>
        <td>$4</td>
        <td>$5</td>
        <td>$6</td>
        <td>$7</td>
        <td><a href=\"form.cgi?action=show_task_result&task=$2&template=$3&group=$4\">执行过程</a></td>
    </tr>

EOF"

    }
    table_task=$(getData Callback "select * from task  ORDER BY id DESC")
    View=$(eval "cat <<EOF
                            $(<template/task-home.html)
EOF")
    eval "cat <<EOF
        $(<template/base.html)
EOF"
    ;;
exec_task)
    getTempate
    getGroup

    View=$(eval "cat <<EOF
    $(<form/exec_task.html)
EOF
    ")
    eval "cat <<EOF
        $(<template/base.html)
EOF"

    ;;
add_group)
    View=$(cat form/add_group.html)
    eval "cat <<EOF
$(<template/base.html)
EOF
# "
    ;;

rollBack)
    Callback() {
        eval "cat <<EOF
                     <tr>
    <td>$1</td>
    <td>$2</td>
    <td>$3</td>
    <td>$4</td>
    <td><a href=\"form.cgi?action=rollBack&project=$2&version=$4\">回滚</a></td>

</tr>

EOF"
    }
    table_rollBack=$(getVersion Callback)
    View=$(eval "cat <<EOF
                            $(<template/rollBack-home.html)
EOF")

    eval "cat <<EOF
        $(<template/base.html)
EOF"

    ;;
*)

    Callback() {
        eval "cat <<EOF
    <tr>
    <td>$1</td>
    <td>$2</td>
    <td><a href=\"form.cgi?action=show_host_info&group=$2\" >关联主机</a></td>
    <td><a href=\"form.cgi?action=delete&db=hostgroup&id=$1\">删除</a></td>
</tr>

EOF"
    }
    table_group=$(getData Callback "select * from hostgroup")
    View=$(eval "cat <<EOF
                            $(<template/hostgroup-home.html)
EOF")

    Callback() {
        eval "cat <<EOF
                      <tr>
    <td>$1</td>
    <td>$2</td>
    <td>$4</td>
    <td>$5</td>
    <td>$6</td>
    <td>$7</td>
    <td><a href=\"form.cgi?action=get_form_host_info&db=host&id=$1\">修改</a>&nbsp;&nbsp;&nbsp;<a
            href=\"form.cgi?action=delete&db=host&id=$1\">删除</a></td>

</tr>

EOF"
    }
    table_host=$(getData Callback "select * from host")
    View=$(eval "cat <<EOF
                            $(<template/hostgroup-home.html)
EOF")

    eval "cat <<EOF
        $(<template/base.html)
EOF"

    ;;

esac
